package io.renren.modules.project.controller;

import com.github.pagehelper.PageInfo;
import io.renren.common.utils.R;
import io.renren.modules.project.model.ProjectBo;
import io.renren.modules.project.model.ProjectDo;
import io.renren.modules.project.model.ProjectVo;
import io.renren.modules.project.service.ProjectService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.task.model.ChinaVo;
import io.renren.modules.task.service.ChinaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 项目表
 *
 * @author zhangquanshuo
 * @date 2020-11-19 16:52:50
 */
@Slf4j
@RestController
@RequestMapping("/business/project")
public class ProjectController extends AbstractController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ChinaService chinaService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("generator:businessproject:list")
    public R list(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                  @RequestParam(value = "pageSize", required = false) Integer pageSize,
                  @RequestParam(value = "projectName", required = false) String projectName,
                  @RequestParam(value = "createTime", required = false) Date createTime,
                  @RequestParam(value = "endTime", required = false) Date endTime,
                  @RequestParam(value = "projectStatus", required = false) Integer projectStatus
    ) {
        Integer  CreateUserId=  Integer.valueOf(String.valueOf(getUserId()));
        PageInfo<ProjectVo> projectVoPageInfo = projectService.queryPage(pageNum, pageSize, projectName, createTime, endTime, projectStatus,CreateUserId);

        if (!projectVoPageInfo.getList().isEmpty()) {
            for (ProjectVo projectVo : projectVoPageInfo.getList()) {
                if (null != projectVo.getProvinceCode()) {
                    ChinaVo chinaVo = chinaService.getById(Long.valueOf(String.valueOf(projectVo.getProvinceCode())));
                    if (null != chinaVo) {
                        projectVo.setProvinceName(chinaVo.getName());
                    }
                }
                if (null != projectVo.getCityCode()) {
                    ChinaVo chinaVo = chinaService.getById(Long.valueOf(String.valueOf(projectVo.getCityCode())));
                    if (null != chinaVo) {
                        projectVo.setCityName(chinaVo.getName());
                    }
                }
                if (null != projectVo.getDistrictCode()) {
                    ChinaVo chinaVo = chinaService.getById(Long.valueOf(String.valueOf(projectVo.getDistrictCode())));
                    if (null != chinaVo) {
                        projectVo.setDistrictName(chinaVo.getName());
                    }
                }
                //用户名
                if (null != projectVo.getCreateUserId()) {
                    SysUserEntity user = sysUserService.getById(projectVo.getCreateUserId());
                    if (null != user) {
                        projectVo.setCreateUserName(user.getUsername());
                    }
                }
            }
        }
        return R.ok().put("page", projectVoPageInfo);
    }

    //只需要加上下面这段即可，注意不能忘记注解
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:project:info")
    public R info(@PathVariable("id") Long id) {
        ProjectDo projectDo = projectService.getById(id);
        return R.ok().put("project", projectDo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("generator:businessproject:save")
    public R save(@RequestBody ProjectBo projectBo, HttpSession session) {
        projectBo.setCreateUserId(Integer.valueOf(String.valueOf(getUserId())));
        log.debug(String.valueOf(getUserId()));
        return R.ok().put("projectData", projectService.save(projectBo));
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("generator:businessproject:update")
    public R update(@RequestBody ProjectBo projectBo) {
        projectService.updateById(projectBo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("generator:businessproject:delete")
    public R delete(@RequestBody Long[] ids) {
        projectService.removeById(ids);
        return R.ok();
    }

    /**
     * 获取文件下载路径
     */
    @GetMapping("/get-down-load-path")
    //@RequiresPermissions("generator:businessproject:delete")
    public R getDownLoadPath(@RequestParam(value = "id") Long id) {
        return R.ok().put("data", projectService.getDownLoadPath(id));
    }

}
